/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.engine;

/**
 * InputPort
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class InputPort extends Port {

	/**
	 * nr
	 */
	private static int nr = 1;

	/**
	 * required
	 */
	private boolean required = false;

	/**
	 * InputPort Constructor
	 * 
	 */
	public InputPort() {
		super("IN" + nr++);
	}

	/**
	 * InputPort Constructor
	 * 
	 * @param name
	 */
	public InputPort(String name) {
		this(name, null);
	}

	/**
	 * InputPort Constructor
	 * 
	 * @param name
	 * @param node
	 */
	public InputPort(String name, AbstractNode node) {
		this(name, node, false);
	}

	/**
	 * InputPort Constructor
	 * 
	 * @param name
	 * @param node
	 * @param required
	 */
	public InputPort(String name, AbstractNode node, boolean required) {
		this(name, node, required, null);
	}

	/**
	 * InputPort Constructor
	 * 
	 * @param name
	 * @param node
	 * @param required
	 * @param type
	 */
	public InputPort(String name, AbstractNode node, boolean required, Class<?> type) {
		super(name, node, type);
		setRequired(required);
	}

	/**
	 * @return true if the buffer contains a filled DataElement (i.e. wrapping a
	 *         non-null value)
	 */
	public boolean hasData() {
		return getBuffer() != null && getBuffer().getData() != null;
	}

	/**
	 * Fetch the DataElement from the input buffer and clear the buffer
	 * automatically
	 * 
	 * @return returns the data in the buffer and clears it
	 */
	public DataElement fetchBuffer() {
		return fetchBuffer(true);
	}

	/**
	 * Fetch the DataElement from the input buffer and optionally clear the
	 * buffer
	 * 
	 * @param clearBuffer
	 * @return
	 */
	public DataElement fetchBuffer(boolean clearBuffer) {
		DataElement de = getBuffer();
		if (clearBuffer) {
			setBuffer(null);
		}
		return de;
	}

	/**
	 * Convenience method to fetch the data object contained in the input
	 * buffer's DataElement. This method clears the input buffer automatically.
	 * 
	 * @return
	 */
	public Object fetchData() {
		return fetchData(true);
	}

	/**
	 * Convenience method to fetch the data object contained in the input
	 * buffer's DataElement. This method optionally clears the input buffer
	 * 
	 * @param clearBuffer
	 * @return
	 */
	public Object fetchData(boolean clearBuffer) {
		DataElement de = fetchBuffer(clearBuffer);
		return de.getData();
	}

	/**
	 * @return the required
	 */
	public boolean isRequired() {
		return required;
	}

	/**
	 * @param required
	 *            the required to set
	 */
	public void setRequired(boolean required) {
		this.required = required;
	}

}
